#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_DISK_002(Case):
    """
        Name:
            REDFISH_DISK_002
        Label:
            硬盘信息（厂商,SN,型号,容量,类型,速率,健康状态等）
        PreCondition:
            BMC web service normal
        TestStep:
            1) get https://BMC_IP/redfish/v1/Chassis/{Chassis_id}/Drives/{Drive_id}（根据集合资源信息，获取相应的硬盘id）
        ExpectedResult:
            A) 执行响应码为200,查询硬盘个数和实际一致
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询硬盘集合资源信息")
        url = "/redfish/v1/Chassis/1/Drives"
        resp = self.request.get(url)
        if resp[0] != 200:
            self.assertIsNotNone(None, f"查询硬盘集合资源信息失败：rc:{resp[0]}\n{json.dump(resp[1], indent=4)}")
        self.logger.info(json.dump(resp[1], indent=4))
        count = resp[1]['Members@odata.count']
        self.logger.info(f"服务器磁盘数量：{count}")



    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
